﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.BLL.App;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.BLL.Tbl;
using HKEA.CMS.Core.BLL.Mtbl;

namespace WebApplication.UserControl
{
    public partial class M_UserMessageControl : System.Web.UI.UserControl
    {
        protected string strImgPath = "";
        protected string strImg = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //整体思路： 先查询app_tblMediaRoom_Related中所有非删除类型的记录集合 A；
                //A集合为空时，直接取 tblMediaRoom_Related最后一条记录对应的MediaRoom信息

                //当 A 集合不为空时，则表示Memeber对相应模块做了修改；判断如若为新增关联，则直接取该记录对应的图片信息(分图片新增、原有图片两种情况),否则为修改信息(分图片新增、原有图片两种情况)
                //由于Logo是显示最后关联表中最后一条记录对应的MediaRoom信息；因此修改关联时，则需要判断 tblMediaRoom_Related 的最后一条记录是否被修改过；  
                //所以 查询出所有MediaRoom_Related所有未删除 且 app表中未做删除标记的所有记录 B；
                //当修改的关联记录为 B 的最新一条记录时，则取最新关联的MediaRoom信息(分图片新增、原有图片两种情况)；
                //否则直接去tblMediaRoom_Related对应的MediaRoom信息

                BasePage basepage = new BasePage();
                tblCustomer customerENT = new tblCustomer();
                customerENT = tblCustomerBLL.GetInstance().GetEntity(basepage.GetEnt());
                //tblCustomer customerENT = basepage.GetCustomerFromSession();
                //var request = Request.QueryString[Constant.UrlParamName.CustomerUID];
                var customeruid = 0;
                if (customerENT != null)
                {
                    customeruid = customerENT.CustomerUID;
                    ltlCompanyName_eng.Text = customerENT.Name_Eng;
                    ltlCompanyName_chi.Text = customerENT.Name_Chi;
                    ltBRNo.Text = customerENT.BR;
                    ltCustomerID.Text = customerENT.CustomerID;
                    ltPassportNo.Text = customerENT.HKID_PassportNo;
                    var TypeENT = customerENT.mtblCustType;
                    if (TypeENT != null)
                    {
                        ltType_Chi.Text = TypeENT.CustType_Chi;
                        ltType_Eng.Text = TypeENT.CustType_Eng;
                    }
                    ltMCategory_Eng.Text = GetMemberStatusOrCategory(customerENT.CustomerUID,2);
                    //Memeber 未审核数据
                    app_tblMediaRoom_Related app_relatedENT = new app_tblMediaRoom_Related();
                    app_relatedENT.RelatedUID = customeruid;
                    app_relatedENT.Deleted = 0;
                    app_relatedENT.Category = Constant.MediaRoomCategory.customer_logo;
                    app_tblMediaRoom_RelatedBLL app_relatedBLL = new app_tblMediaRoom_RelatedBLL();
                    var app_list = app_relatedBLL.GetMaxUIDRelatedENTByCustomerUID(app_relatedENT);

                    //Memeber 审核后数据
                    tblMediaRoom_Related relatedENT = new tblMediaRoom_Related();
                    relatedENT.RelatedUID = customeruid;
                    relatedENT.Category = Constant.MediaRoomCategory.customer_logo;
                    relatedENT.Deleted = 0;
                    tblMediaRoomRelatedBLL relatedBLL = new tblMediaRoomRelatedBLL();
                    tblMediaRoom_Related MaxMediaRoom_RelatedENT = null;
                    var list = relatedBLL.GetListWithOutAppDelByENT(relatedENT);
                    if (list.Count > 0)
                        MaxMediaRoom_RelatedENT = list.First();
                    if (app_list.Count > 0) //Member有操作
                    {
                        var added_app_list = app_list.Where(a => !(a.MediaRoomRelatedUID > 0));
                        if (added_app_list.Count() > 0) //如果MediaRoomRelatedUID 为空，则为新增
                        {
                            var add_app_relatedENT = added_app_list.First(); //被新增的实体
                            string strPath = GetImgPathFromAppRelated(add_app_relatedENT);
                            if (!string.IsNullOrEmpty(strPath))
                                strImgPath = strPath;
                        }
                        else //否则为修改
                        {
                            //当修改内容为 Memeber Logo关联的最后一条记录时
                            if (MaxMediaRoom_RelatedENT != null && app_list.Where(a => a.MediaRoomRelatedUID == MaxMediaRoom_RelatedENT.MediaRoomRelatedUID).Count() > 0)
                            {
                                var Update = app_list.Where(a => a.MediaRoomRelatedUID == MaxMediaRoom_RelatedENT.MediaRoomRelatedUID).First();
                                string strPath = GetImgPathFromAppRelated(Update);
                                if (!string.IsNullOrEmpty(strPath))
                                    strImgPath = strPath;
                            }
                            else
                            {
                                if (MaxMediaRoom_RelatedENT != null)
                                {
                                    var MediaRoom = MaxMediaRoom_RelatedENT.tblMediaRoom;
                                    if (MediaRoom != null)
                                        strImgPath = MediaRoom.FilePath + MediaRoom.FileName + "_100_100.jpg";
                                }
                            }
                        } 
                    }
                    else //Member无操作
                    {
                        if (MaxMediaRoom_RelatedENT != null)
                        {
                            var MediaRoom = MaxMediaRoom_RelatedENT.tblMediaRoom;
                            if (MediaRoom != null)
                                strImgPath = MediaRoom.FilePath + MediaRoom.FileName + "_100_100.jpg";
                        }
                    }

                    if (!string.IsNullOrEmpty(strImgPath))
                        strImg = "<img id=\"ContentPlaceHolder2_usermeg_imgLogo\" src=\"" + strImgPath + "\"></img>";
                }
            }
        }

        private string GetImgPathFromAppRelated(app_tblMediaRoom_Related ent)
        {
            string str = string.Empty;
            if (ent.MediaRoomUID > 0) //MediaRoomUID表示为图片为原有存在图片
            {
                var tempMediaRoomENT = ent.tblMediaRoom;
                strImgPath = tempMediaRoomENT.FilePath + tempMediaRoomENT.FileName + "_100_100.jpg";
            }
            else if (ent.AppMediaRoomUID > 0) // AppMediaRoomUID > 0表示图片为新增图片
            {
                app_tblMediaRoom appMediaRoom = new app_tblMediaRoom();
                appMediaRoom.AppMediaRoomUID = (int)ent.AppMediaRoomUID;
                str = GetImgPathFromApp(appMediaRoom);
            }
            return str;
        }

        private string GetImgPathFromApp(app_tblMediaRoom ent)
        {
            string str = string.Empty;
            app_tblMediaRoom app_MediaRoomENT = new app_tblMediaRoom();
            app_tblMediaRoomBLL app_MediaRoomBLL = new app_tblMediaRoomBLL();
            var app_MediaRoomList = app_MediaRoomBLL.GetListByENT(ent);
            if (app_MediaRoomList.Count > 0)
            {
                var tempMediaRoomENT = app_MediaRoomList.First();
                str = tempMediaRoomENT.FilePath + tempMediaRoomENT.FileName + "_100_100.jpg";
            }
            return str;
        }

        private string GetMemberStatusOrCategory(int customerUID, int type)
        {
            string ret = string.Empty;
            tblCust_Member entMember = GetMemberByCustomerUID(customerUID);
            if (entMember != null)
            {
                if (type == 1)
                {
                    int tmp_uid = int.Parse(entMember.MemStatusUID.ToString());
                    ret = mtblMembershipStatusBLL.GetInstance().GetMembershipStatus(tmp_uid);
                }
                else if (type == 2)
                {
                    ret = mtblMembershipCategoryBLL.GetInstance().GetMemCategoryByMemberUID(entMember.MemberUID);
                }
            }
            return ret;
        }
        private tblCust_Member GetMemberByCustomerUID(int customerUID)
        {
            tblCust_Member entMember = new tblCust_Member();
            entMember.CustomerUID = customerUID;
            List<tblCust_Member> listMember = new List<tblCust_Member>();
            listMember = tblCust_MemberBLL.GetInstance().GetListByENT(entMember);
            if (listMember.Count() > 0)
                entMember = listMember[0];
            else
                entMember = null;
            return entMember;
        }
    }
}